<?php

namespace Home\Controller;
use Home\Controller\BaseController;
class MyAssetController extends BaseController{
    public function index(){
        $user_id=session('id');
        $search=$_GET['search'];
        $check_status=$_GET['check_status'];
        $get_status=$_GET['get_status'];
        $status=$_GET['status'];
        $year=$_GET['year'];
        $term=$_GET['term'];

//        var_dump($check_status);
//        var_dump($get_status);

//        获取所有的学年和学期
        $now_term=M('term_time')->field('xueyear')->limit(6)->select();
//        处理学年下拉框
        $this->assign('now_term',$now_term);
        //        列表数据处理
        if (isset($search) && !empty($search)) {
            $where['asset_name'] = ['like', '%' . $search . '%'];
        }
        if (isset($search) && $search === '0') {
            $where['asset_name'] = ['like', '%' . $search . '%'];
        }
        if (isset($check_status) && in_array($check_status, [1, 2])) {
            $where['check_status'] = intval($check_status);
        }
        if (isset($get_status) && in_array($get_status, [1, 2])) {
            $where['get_status'] = intval($get_status);
        }
//        归还状态
        if (isset($status) && in_array($status, [1, 2])) {
            $where['status'] = intval($status);
        }
//        学年
        if (isset($year) && !empty($year)) {
            $where['xueyear'] = intval($year);
        }
        if (isset($term) && !empty($term)) {
            $where['term'] = intval($term);
        }

        $where['user_id']=$user_id;

        $field = 'id,asset_name,pattern,count,check_status,get_status,status,use_address,use,create_time,backed_time';

        $result = M('asset_lend')->field($field)->where($where)->select();
        $count = count($result);

        $Page = new \Think\Page($count, 5);

        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $Page->setConfig('next', '上一页');
        $Page->setConfig('prev', '下一页');
        $show = $Page->show();

        var_dump($where);

        $asset_lend = M('asset_lend')->field($field)->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('count', $count);
        $this->assign('show', $show);
        $this->assign('asset_lend', $asset_lend);
//        保存默认值
        if (isset($get_status) && in_array($get_status, [0, 1, 2])) {
            $this->assign('get_status', $get_status);
        }
        if (isset($check_status) && in_array($check_status, [0, 1, 2])) {
            $this->assign('check_status', $check_status);
        }
        if (isset($status) && in_array($status, [0, 1, 2])) {
            $this->assign('status', $status);
        }
        if (isset($search)) {
            $this->assign('name', $search);
        }
        if (isset($year)) {
            $this->assign('year', $year);
        }
        if (isset($term)) {
            $this->assign('term', $term);
        }
        $this->display('teacher/myAsset');

//        列表数据处理结束
    }
}